package com.wcq.studentbackend.controller;

import com.wcq.studentbackend.dto.ExamDTO; // 假设您有这个 DTO
import com.wcq.studentbackend.dto.ExamRequestDTO; // 假设您有这个 DTO
import com.wcq.studentbackend.service.ExamService; // 假设您有这个 Service
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
// import javax.validation.Valid; // 如果需要校验
@Slf4j
@Api(value = "考试管理")
@RestController
@RequestMapping("/api/exams")
public class ExamController {

    private final ExamService examService;

    @Autowired
    public ExamController(ExamService examService) {
        this.examService = examService;
    }

    @GetMapping
    @ApiOperation(value = "获取所有考试信息接口")
    public ResponseEntity<List<ExamDTO>> getAllExams() {
        log.info("Getting all exams");
        List<ExamDTO> exams = examService.getAllExams();
        return ResponseEntity.ok(exams);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取考试详细信息接口")
    public ResponseEntity<ExamDTO> getExamById(@PathVariable Long id) {
        log.info("Getting exam by ID: {}", id);
        ExamDTO examDTO = examService.getExamById(id);
        if (examDTO != null) {
            return ResponseEntity.ok(examDTO);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    @ApiOperation(value = "创建考试接口")
    public ResponseEntity<ExamDTO> createExam(/*@Valid*/ @RequestBody ExamRequestDTO examRequestDTO) {
        log.info("Creating exam, parameters={}", examRequestDTO);
        ExamDTO createdExam = examService.createExam(examRequestDTO);
        // 通常创建成功返回 201 Created
        return ResponseEntity.status(HttpStatus.CREATED).body(createdExam);
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新考试接口")
    public ResponseEntity<ExamDTO> updateExam(@PathVariable Long id, /*@Valid*/ @RequestBody ExamRequestDTO examRequestDTO) {
        log.info("Updating exam with ID: {}, parameters={}", id, examRequestDTO);
        ExamDTO updatedExam = examService.updateExam(id, examRequestDTO);
        if (updatedExam != null) {
            return ResponseEntity.ok(updatedExam);
        }
        return ResponseEntity.notFound().build(); // 或者根据业务逻辑返回其他错误码
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除考试接口")
    public ResponseEntity<Void> deleteExam(@PathVariable Long id) {
        log.info("Deleting exam with ID: {}", id);
        boolean deleted = examService.deleteExam(id);
        if (deleted) {
            return ResponseEntity.noContent().build(); // 204 No Content
        }
        return ResponseEntity.notFound().build();
    }
} 